<?php
// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view' );

class QcalcViewMarshrutization extends Jview {

	function display($tpl = null) {

		$model	=& $this->getModel();
		$user    =& JFactory::getUser();

      $this->assignRef('user',   $user);

		SWITCH ( JRequest::getVar("task") ) {

         CASE "load_marshrut_list_recs":
            $id = (int)$_POST['id'];
            $list    = $model->getMarshrutListById($user->id,$id);
            $listRec = $model->getMarshrutRecById($user->id,$id);
            $this->assignRef('list',   $list[0]);
            $this->assignRef('listRec',$listRec);
            break;
            
         CASE "add_marshrut_list":
            $list->id      = 0;
            $list->name    = JRequest::getVar('name','','post','db_string');
            $list->date    = date('Y-m-d h:i:s');
            $list->user_id = $user->id;
            $list->id = $model->addMList($list);
            if ($list->id) {
               $this->assignRef('list', $list);
            } else {
               die('alert("Ошибка сохранения листа");'); }
            break;
            
         CASE "del_marshrut_list":
            $list_id = (int)$_POST['id'];
            
            $model->delMList($user,$list_id);

            die(" var d_status = true; ");
            break;
            
         CASE "add_marshrut_list_rec":
            $calc_id       = (int)$_POST['calcs'];
            $poluchatel_id = (int)$_POST['poluchatel'];
            $list_id       = (int)$_POST['list_id'];
            
            if ($list_id) {
               $lits   = $model->getMarshrutRecById($user->id,$list_id);
               $rec_id = count($lits) ? $lits[0]->rec_id +1 : 1; // если есть записи то инкрементируем максимальную...
            } else {
               die('alert("Ошибка открытия листа");'); }
            
            $calc = null;
            if ($calc_id) {
               $calc = $model->getCalcsById($user->id,$calc_id);
               $calc = @$calc[0];
               if ($calc) {
                  $calc->marshrut = unserialize($calc->marshrut);
                  $calc->gruz     = unserialize($calc->gruz);
                  $calc->calc     = unserialize($calc->calc);
               }
            }
            
            $poluchatel = null;
            if ($poluchatel_id) {
               $poluchatel = $model->getPoluchatelById($user->id,$poluchatel_id);
               $poluchatel = @$poluchatel[0];
            }
            
            $company = @$model->getCompanyById($calc->calc->company_id);
            $company = @$company[0];
            
            $rec = $model->addMListRec($list_id,$rec_id,$user,$company,$poluchatel,$calc);
            
            $this->assignRef('list_id',    $list_id);
            $this->assignRef('rec_id',     $rec_id);
            $this->assignRef('calc',       $calc);
            $this->assignRef('poluchatel', $poluchatel);
            $this->assignRef('company',    $company);
            break;
            
         CASE "del_marshrut_list_rec":
            $rec_id  = (int)$_POST['rec_id'];
            $list_id = (int)$_POST['list_id'];
            
            $model->delMListRec($user,$list_id,$rec_id);

            die("jQuery('#MList_row_$rec_id > TD').css('border','1px solid red'); jQuery('#MList_row_$rec_id').hide(800, function() { jQuery('#MList_row_$rec_id').remove(); });");
            break;
            
         CASE "marsrut_to_history":
            $list_id = (int)$_POST['id'];
            
            $model->toHistoryMList($user->id,$list_id);

            die(" var h_status = true; ");
            break;

         CASE "save_marsrut_list_all":
            $id = (int)$_POST['id']; //JRequest::getVar('desc', '', 'post', 'db_string');
            $listRec = $model->getMarshrutRecById($user->id,$id);

            foreach ($listRec AS &$r) {
               $r = $this->getPostParams($r);
               $model->saveMListRec($r);
            }
            die('alert(\'Лист отправки сохранен\');');
            //echo "/*". print_r($_POST,1) . print_r($listRec,1) ."*/"; die();
            break;

         CASE "print_marsrut_list":
            $id = (int)$_POST['id'];
            $list    = $model->getMarshrutListById($user->id,$id);
            $listRec = $model->getMarshrutRecById($user->id,$id);

            foreach ($listRec AS &$r) {
               $r = $this->getPostParams($r);
               //$model->saveMListRec($r);
            }
            $this->assignRef('list',   $list[0]);
            $this->assignRef('listRec',$listRec);
            //echo "<pre>". print_r($listRec,1) ."</pre>"; die();
            break;

         CASE "print_yarlik":
            $id = (int)$_POST['id'];
            $list    = $model->getMarshrutListById($user->id,$id);
            $listRec = $model->getMarshrutRecById($user->id,$id);

            foreach ($listRec AS &$r) {
               $r = $this->getPostParams($r);
               //$model->saveMListRec($r);
            }
            $this->assignRef('list',   $list[0]);
            $this->assignRef('listRec',$listRec);
            //echo "<pre>". print_r($listRec,1) ."</pre>"; die();
            break;
            
         DEFAULT:
            //echo "<pre>"; print_r($this);
            die('404 NOT Found... ['.JRequest::getVar("task") ."|". $tpl .']');
		}

		parent::display($tpl);
   }

   function getPostParams($r) {
      $r->otpr_city    = JRequest::getVar('otpr_city_'.   $r->rec_id, '', 'post', 'db_string');
      $r->otpr_comp    = JRequest::getVar('otpr_comp_'.   $r->rec_id, '', 'post', 'db_string');
      $r->otpr_name    = JRequest::getVar('otpr_name_'.   $r->rec_id, '', 'post', 'db_string');
      $r->otpr_tel     = JRequest::getVar('otpr_tel_'.    $r->rec_id, '', 'post', 'db_string');
      $r->per_name     = JRequest::getVar('per_name_'.    $r->rec_id, '', 'post', 'db_string');
      $r->per_adres    = JRequest::getVar('per_adres_'.   $r->rec_id, '', 'post', 'db_string');
      $r->per_tel      = JRequest::getVar('per_tel_'.     $r->rec_id, '', 'post', 'db_string');
      $r->poluch_city  = JRequest::getVar('poluch_city_'. $r->rec_id, '', 'post', 'db_string');
      $r->poluch_adres = JRequest::getVar('poluch_adres_'.$r->rec_id, '', 'post', 'db_string');
      $r->poluch_name  = JRequest::getVar('poluch_name_'. $r->rec_id, '', 'post', 'db_string');
      $r->poluch_tel   = JRequest::getVar('poluch_tel_'.  $r->rec_id, '', 'post', 'db_string');
      $r->kolvo_mest   = JRequest::getVar('kolvo_mest_'.  $r->rec_id, '', 'post', 'db_string');
      return $r;
   }

}